home *** CD-ROM | disk | FTP | other *** search
/ A Teacher's Guide to the Holocaust / A Teacher's Guide to the Holocaust.iso / data / people / scripts / elemibtnclass.js < prev    next >
Text File  |  1999-12-05  |  6KB  |  214 lines

  1. // Copyright 1998,1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //Constructs a image button element
  4. function MM_ibtn(theParent, theName, theInitialValue,
  5.                  theExpectedValue, theIsCorrect, theScore,
  6.                  theIsToggle, theStateMask)
  7. {
  8.   // properties
  9.   this.initialValue = theInitialValue;
  10.   this.value = '';
  11.   this.disabled = true;
  12.  
  13.   this.expectedValue = theExpectedValue;
  14.   this.isCorrect = theIsCorrect;
  15.   this.score = theScore;
  16.   this.selected = false;
  17.  
  18.   this.isToggle = theIsToggle;
  19.  
  20.   this._parent = theParent;
  21.   this._name = theName;
  22.   this._obj = '';
  23.   this._stateMask = theStateMask ? theStateMask : '';
  24.   this._mouseOver = false;
  25.   this._mouseDown = false;
  26.   this._filePre = '';
  27.   this._fileExt = '';
  28.  
  29.   this.c = new Array(this); // NOTE: choice info stored on the element.
  30.  
  31.   // member functions
  32.   this.init = MM_ibtnInit;
  33.   this.reset = MM_ibtnReset;
  34.   this.enable = MM_ibtnEnable;
  35.   this.disable = MM_ibtnDisable;
  36.   this.setDisabled = MM_ibtnSetDisabled;
  37.   this.update = MM_ibtnUpdate;
  38.   this.redraw = MM_ibtnRedraw;
  39.   this.validValue = MM_ibtnValidValue;
  40.   this.changeValue = MM_ibtnChangeValue;
  41.   this.setValue = MM_ibtnSetValue;
  42.   this.setSelected = MM_ibtnSetSelected;
  43.   this.setIsToggle = MM_ibtnSetIsToggle;
  44. }
  45.  
  46. // Initializes the element
  47. function MM_ibtnInit() {
  48.   var theSrc, extIndex, maskArray, extArray, i;
  49.   with (this) {
  50.     if (! _stateMask) return;
  51.  
  52.     _obj = MM_intFindObject(_parent._self + _name + "Btn");
  53.     if (_obj && _obj.src != null) {
  54.       theSrc = _obj.src;
  55.       extIndex = theSrc.lastIndexOf(".");
  56.       if (extIndex != -1) { // save the extension
  57.         _fileExt = theSrc.substring(extIndex, theSrc.length);
  58.         theSrc = theSrc.substring(0, extIndex); // remove extension
  59.       }
  60.       if (theSrc.lastIndexOf("_sel") != -1)
  61.         _filePre = theSrc.substring(0, theSrc.lastIndexOf("_sel"));
  62.       else if (theSrc.lastIndexOf("_hlt") != -1)
  63.         _filePre = theSrc.substring(0, theSrc.lastIndexOf("_hlt"));
  64.       else if (theSrc.lastIndexOf("_dis") != -1)
  65.         _filePre = theSrc.substring(0, theSrc.lastIndexOf("_dis"));
  66.       else
  67.         _filePre = theSrc;
  68.         
  69.       // preload the images
  70.       maskArray = new Array('s', 'S', 'h', 'H', 'd', 'D');
  71.       extArray = new Array("", "_sel", "_hlt", "_sel_hlt", "_dis", "_sel_dis"); 
  72.       for (i=0; i < maskArray.length; i++)
  73.         if (_stateMask.indexOf(maskArray[i]) != -1)
  74.           MM_preloadImages(_filePre + extArray[i] + _fileExt);
  75.   } }
  76. }
  77.  
  78. //Resets the element
  79. function MM_ibtnReset() {
  80.   var isChanged = '';
  81.   with (this) {
  82.     isChanged = (value != initialValue);
  83.     _mouseOver = false;
  84.     _mouseDown = false;
  85.     value = initialValue;
  86.     _parent.disabled ? disable() : enable();
  87.     validValue();
  88.     redraw();
  89.     if (isChanged && this.onChange != null) onChange(_parent._self+_name, value);
  90.   }
  91. }
  92.  
  93. //Enables the element
  94. function MM_ibtnEnable() {
  95.   if (this._obj) with (this) {
  96.     disabled = false;
  97.     redraw();
  98.   }
  99. }
  100.  
  101. //Disables the element
  102. function MM_ibtnDisable() {
  103.   this.disabled = true;
  104.   this.redraw();
  105. }
  106.  
  107. //Calls the approppriate disable or enable function
  108. function MM_ibtnSetDisabled(theDisabled) {
  109.   if (theDisabled) this.disable();
  110.   else this.enable();
  111. }
  112.  
  113. //Called by the onClick, onMouseOver, onMouseDown, and onMouseOut
  114. // events of the A tag, to change the button image and state
  115. function MM_ibtnUpdate(theEvent) {
  116.   if (!this.disabled) with (this) {
  117.     if (theEvent != "onclick") {
  118.       if (theEvent == "onmouseover") {
  119.         if (_mouseOver) return;
  120.         _mouseOver = true;
  121.       } else if (theEvent == "onmouseout") {
  122.         if (!_mouseDown && !_mouseOver) return;
  123.         _mouseDown = false;
  124.         _mouseOver = false;
  125.       } else if (theEvent == "onmousedown") {
  126.         if (_mouseDown) return;
  127.         _mouseDown = true;
  128.       }
  129.       redraw();
  130.     } else { // onclick
  131.       _mouseDown = false;
  132.       changeValue((_parent.allowMultiSel) ? !value : true);
  133.       _parent.update();
  134.   } }
  135. }
  136.  
  137. // Sets the image based on the button state
  138. function MM_ibtnRedraw() {
  139.   if (this._filePre) with (this) {
  140.     var imageIndex = 's';
  141.     var imageExt = '';
  142.     if (disabled) {
  143.       imageIndex = 'd';
  144.       imageExt += "_dis";
  145.     } else if (_mouseOver) {
  146.       imageIndex = 'h';
  147.       imageExt += "_hlt";
  148.     }
  149.     if ((value && isToggle) || _mouseDown) {
  150.       if (_stateMask.indexOf(imageIndex.toUpperCase()) != -1) {
  151.         imageExt = "_sel" + imageExt;
  152.       } else if (_stateMask.indexOf(imageIndex) == -1) {
  153.         // unselected images not found
  154.         if (_stateMask.indexOf('S') != -1) imageExt = "_sel";
  155.         else imageExt = '';
  156.       }
  157.     } else if (_stateMask.indexOf(imageIndex) == -1) imageExt = '';
  158.  
  159.     var currImageName = _obj.src;
  160.     var imageName = _filePre + imageExt + _fileExt;
  161.     if (currImageName != imageName) _obj.src = imageName;
  162.   }
  163. }
  164.  
  165. function MM_ibtnValidValue() {
  166.   this.selected = (this.value == this.expectedValue);
  167.   return this.selected;
  168. }
  169.  
  170. function MM_ibtnChangeValue(theValue) {
  171.   var i, isChanged = '', isReset = '';
  172.   with (this) {
  173.     isChanged = (value != theValue);
  174.     if (!_parent.allowMultiSel) {
  175.       value = theValue;
  176.       for (i in _parent.e) if (i != 'length') with (_parent) {
  177.         if (e[i] != this) {
  178.           isReset = (e[i].value != false);
  179.           e[i].value = false;
  180.         }
  181.         e[i].validValue();
  182.         e[i].redraw();
  183.         if (e[i] != this && isReset && e[i].onChange != null) 
  184.           e[i].onChange(e[i]._parent._self+e[i]._name, e[i].value);
  185.       }
  186.     } else {
  187.       value = theValue;
  188.       validValue();
  189.       redraw();
  190.     }
  191.     if (isChanged && this.onChange != null) onChange(_parent._self+_name, value);
  192.   }
  193. }
  194.  
  195. function MM_ibtnSetValue(theValue) {
  196.   with (this) {
  197.     changeValue(theValue);
  198.     _parent.update(true); // update int, but don't judge
  199.   }
  200. }
  201.  
  202. function MM_ibtnSetSelected(theSelected) {
  203.   if (theSelected)
  204.     this.setValue(this.expectedValue);
  205.   else
  206.     this.setValue(!this.expectedValue);
  207. }
  208.  
  209. function MM_ibtnSetIsToggle(theIsToggle) {
  210.   with (this) {
  211.     isToggle = theIsToggle;
  212.     redraw();
  213.   }
  214. }